package com.adv.util;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Properties;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

/**
 * 
 * @ClassName: PropertiesUtil
 * @Description: 属性文件工具类，可以获取类路径下所有属性文件
 * @author: WUQINGLONG
 * @date: 2016年9月20日 下午5:31:25
 */
public class PropertiesUtil {

	public static Properties properties = new Properties();

	static {
		try {
			// 加载classpath路径下所有properties文件
			File classPathFile = new ClassPathResource("").getFile();
			File[] propertiesFileArr = classPathFile.listFiles(new FilenameFilter() {
				@Override
				public boolean accept(File dir, String name) {
					return name.endsWith(".properties");
				}
			});
			if (null != propertiesFileArr) {
				Properties singleProps;
				for (File propertiesFile : propertiesFileArr) {
					singleProps = new Properties();
					singleProps.load(new FileSystemResource(propertiesFile).getInputStream());
					properties.putAll(singleProps);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String get(String key, String defVal) {
		return properties.getProperty(key, defVal);
	}

	public static String get(String key) {
		return properties.getProperty(key);
	}

	public static void main(String[] args) throws Exception {
		System.out.println(PropertiesUtil.get("mail.username"));
	}
}